package com.antis.olsl.magic.report.daily;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.android.arouter.launcher.ARouter;
import com.antis.olsl.arouterlib.MagicRoutePath;
import com.antis.olsl.base.BaseFragment;
import com.antis.olsl.base.utils.ToastUtil;
import com.antis.olsl.magic.databinding.FragmentDailyBinding;
import com.antis.olsl.magic.response.GetSalesSummaryResp;
import com.trello.rxlifecycle2.LifecycleTransformer;

import java.util.HashMap;
import java.util.Map;

import timber.log.Timber;

/**
 * 销售报表-日报Fragment
 *
 * @author hhh
 */
public class DailyFragment extends BaseFragment<FragmentDailyBinding> implements DailyContract.View {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private static final String ARG_PARAM3 = "param3";

    private String mSalesRoomId;
    private String mSearchDate;
    private String mShowDate;

    private GetSalesSummaryResp.ContentBean mBean;

    private DailyPresenter mPresenter;

    public DailyFragment() {
    }

    public static DailyFragment newInstance(String roomId, String date, String showDate) {
        DailyFragment fragment = new DailyFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, roomId);
        args.putString(ARG_PARAM2, date);
        args.putString(ARG_PARAM3, showDate);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mSalesRoomId = getArguments().getString(ARG_PARAM1);
            mSearchDate = getArguments().getString(ARG_PARAM2);
            mShowDate = getArguments().getString(ARG_PARAM3);
        }
    }

    @Override
    protected FragmentDailyBinding getViewBinding() {
        return FragmentDailyBinding.inflate(getLayoutInflater(), baseBinding.getRoot(), true);
    }

    @Override
    protected void initView(View view) {
        initPresenter();

        Timber.tag("hhh").e("daily initView");

        viewBinding.textDetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = "";
                if (null != mBean) {
                    name = mBean.salesroom_name;
                }
                ARouter.getInstance()
                        .build(MagicRoutePath.ACTIVITY_DAILY_DETAIL)
                        .withString("salesRoomId", mSalesRoomId)
                        .withString("searchDate", mSearchDate)
                        .withString("showDate", mShowDate)
                        .withString("salesRoomName", name)
                        .navigation();
            }
        });
    }

    private void initPresenter() {
        mPresenter = new DailyPresenter();
        mPresenter.takeView(this);
    }

    @Override
    protected void loadData() {
        loadData(mSalesRoomId, mSearchDate, mShowDate);
        Timber.tag("hhh").e("daily loadData");
    }

    public void loadData(String salesroomId, String searchDate, String strTextDate) {
        mSalesRoomId = salesroomId;
        mSearchDate = searchDate;
        mShowDate = strTextDate;
        if (TextUtils.isEmpty(salesroomId)) {
            salesroomId = "";
        }
        Map<String, Object> map = new HashMap<>(3);
        map.put("shopId", salesroomId);
        map.put("searchTime", searchDate);
        mPresenter.getDailySalesSummary(map);
    }

    @Override
    public void getDailySalesSummarySuccess(GetSalesSummaryResp.ContentBean bean) {
        if (null != bean) {
            mBean = bean;

            float dailyProgress = 0f;
            if (!TextUtils.isEmpty(bean.rmbdcl)) {
                try {
                    dailyProgress = Float.parseFloat(bean.rmbdcl);
                } catch (NumberFormatException e) {
                    Timber.tag("hhh").e("exception= " + e);
                }
            }
            viewBinding.dailySeekBar.setProgress(dailyProgress);
            float weeklyProgress = 0f;
            if (!TextUtils.isEmpty(bean.zmbdcl)) {
                try {
                    weeklyProgress = Float.parseFloat(bean.zmbdcl);
                } catch (NumberFormatException e) {
                    Timber.tag("hhh").e("exception= " + e);
                }
            }
            viewBinding.weeklySeekBar.setProgress(weeklyProgress);

            // 销售金额
            setSalesAmountValue(bean);

            // 销售数量
            setSalesVolumeValue(bean);

            // 会员销售金额
            setMemberAmountValue(bean);

            // 会员销售数量
            setMemberVolumeValue(bean);

            // 连带率
            setJointRateValue(bean);

            // 客单价
            setCustomerPriceValue(bean);

            // 明星单品
            setStarSingleProductValue(bean);

            // 支单价
            setUnitPriceValue(bean);
        }
    }

    private void setSalesAmountValue(GetSalesSummaryResp.ContentBean bean) {
        if (!TextUtils.isEmpty(bean.xsje)) {
            viewBinding.layoutSalesAmount.textSalesAmount.setText(bean.xsje);
        } else {
            viewBinding.layoutSalesAmount.textSalesAmount.setText("0");
        }

        if (!TextUtils.isEmpty(bean.xsjezb)) {
            String salesProportion = bean.xsjezb + "%";
            viewBinding.layoutSalesAmount.textSalesProportion.setText(salesProportion);
        } else {
            viewBinding.layoutSalesAmount.textSalesProportion.setText("-");
        }

        if (!TextUtils.isEmpty(bean.hbxsje)) {
            String salesMom = bean.hbxsje + "%";
            viewBinding.layoutSalesAmount.textSalesMom.setText(salesMom);
        } else {
            viewBinding.layoutSalesAmount.textSalesMom.setText("-");
        }

        if (!TextUtils.isEmpty(bean.tbxsje)) {
            String salesYoy = bean.tbxsje + "%";
            viewBinding.layoutSalesAmount.textSalesYoy.setText(salesYoy);
        } else {
            viewBinding.layoutSalesAmount.textSalesYoy.setText("-");
        }
    }

    private void setSalesVolumeValue(GetSalesSummaryResp.ContentBean bean) {
        if (!TextUtils.isEmpty(bean.xssl)) {
            viewBinding.layoutSalesVolume.textSalesVolume.setText(bean.xssl);
        } else {
            viewBinding.layoutSalesVolume.textSalesVolume.setText("0");
        }

        if (!TextUtils.isEmpty(bean.xsslzb)) {
            String volumeProportion = bean.xsslzb + "%";
            viewBinding.layoutSalesVolume.textVolumeProportion.setText(volumeProportion);
        } else {
            viewBinding.layoutSalesVolume.textVolumeProportion.setText("-");
        }

        if (!TextUtils.isEmpty(bean.hb_xssl)) {
            String volumeMom = bean.hb_xssl + "%";
            viewBinding.layoutSalesVolume.textVolumeMom.setText(volumeMom);
        } else {
            viewBinding.layoutSalesVolume.textVolumeMom.setText("-");
        }

        if (!TextUtils.isEmpty(bean.tbxssl)) {
            String volumeYoy = bean.tbxssl + "%";
            viewBinding.layoutSalesVolume.textVolumeYoy.setText(volumeYoy);
        } else {
            viewBinding.layoutSalesVolume.textVolumeYoy.setText("-");
        }
    }

    private void setMemberAmountValue(GetSalesSummaryResp.ContentBean bean) {
        if (!TextUtils.isEmpty(bean.hyxsje)) {
            viewBinding.layoutMemberSalesAmount.textMemberSalesAmount.setText(bean.hyxsje);
        } else {
            viewBinding.layoutMemberSalesAmount.textMemberSalesAmount.setText("0");
        }

        if (!TextUtils.isEmpty(bean.hyxsjezb)) {
            String memberSalesProportion = bean.hyxsjezb + "%";
            viewBinding.layoutMemberSalesAmount.textMemberSalesProportion.setText(memberSalesProportion);
        } else {
            viewBinding.layoutMemberSalesAmount.textMemberSalesProportion.setText("-");
        }

        if (!TextUtils.isEmpty(bean.hbhyxsje)) {
            String memberSalesMom = bean.hbhyxsje + "%";
            viewBinding.layoutMemberSalesAmount.textMemberSalesMom.setText(memberSalesMom);
        } else {
            viewBinding.layoutMemberSalesAmount.textMemberSalesMom.setText("-");
        }

        if (!TextUtils.isEmpty(bean.tbhyxsje)) {
            String memberSalesYoy = bean.tbhyxsje + "%";
            viewBinding.layoutMemberSalesAmount.textMemberSalesYoy.setText(memberSalesYoy);
        } else {
            viewBinding.layoutMemberSalesAmount.textMemberSalesYoy.setText("-");
        }
    }

    private void setMemberVolumeValue(GetSalesSummaryResp.ContentBean bean) {
        if (!TextUtils.isEmpty(bean.hyxssl)) {
            viewBinding.layoutMemberSalesVolume.textMemberSalesVolume.setText(bean.hyxssl);
        } else {
            viewBinding.layoutMemberSalesVolume.textMemberSalesVolume.setText("0");
        }

        if (!TextUtils.isEmpty(bean.hyxsslzb)) {
            String memberVolumeProportion = bean.hyxsslzb + "%";
            viewBinding.layoutMemberSalesVolume.textMemberVolumeProportion.setText(memberVolumeProportion);
        } else {
            viewBinding.layoutMemberSalesVolume.textMemberVolumeProportion.setText("-");
        }

        if (!TextUtils.isEmpty(bean.hbhyxssl)) {
            String memberVolumeMom = bean.hbhyxssl + "%";
            viewBinding.layoutMemberSalesVolume.textMemberVolumeMom.setText(memberVolumeMom);
        } else {
            viewBinding.layoutMemberSalesVolume.textMemberVolumeMom.setText("-");
        }

        if (!TextUtils.isEmpty(bean.tbhyxssl)) {
            String memberVolumeYoy = bean.tbhyxssl + "%";
            viewBinding.layoutMemberSalesVolume.textMemberVolumeYoy.setText(memberVolumeYoy);
        } else {
            viewBinding.layoutMemberSalesVolume.textMemberVolumeYoy.setText("-");
        }
    }

    private void setJointRateValue(GetSalesSummaryResp.ContentBean bean) {
        viewBinding.layoutJointRate.textJointRate.setText(bean.ldl);
        if (!TextUtils.isEmpty(bean.hb_ldl)) {
            String jointRateMom = bean.hb_ldl + "%";
            viewBinding.layoutJointRate.textJointRateMom.setText(jointRateMom);
        } else {
            viewBinding.layoutJointRate.textJointRateMom.setText("-");
        }

        if (!TextUtils.isEmpty(bean.tbldl)) {
            String jointRateYoy = bean.tbldl + "%";
            viewBinding.layoutJointRate.textJointRateYoy.setText(jointRateYoy);
        } else {
            viewBinding.layoutJointRate.textJointRateYoy.setText("-");
        }
    }

    private void setCustomerPriceValue(GetSalesSummaryResp.ContentBean bean) {
        if (!TextUtils.isEmpty(bean.kdj)) {
            viewBinding.layoutCustomerPrice.textCustomerPrice.setText(bean.kdj);
        } else {
            viewBinding.layoutCustomerPrice.textCustomerPrice.setText("0");
        }

        if (!TextUtils.isEmpty(bean.hb_kdj)) {
            String customerPriceMom = bean.hb_kdj + "%";
            viewBinding.layoutCustomerPrice.textCustomerPriceMom.setText(customerPriceMom);
        } else {
            viewBinding.layoutCustomerPrice.textCustomerPriceMom.setText("-");
        }

        if (!TextUtils.isEmpty(bean.tbkdj)) {
            String customerPriceYoy = bean.tbkdj + "%";
            viewBinding.layoutCustomerPrice.textCustomerPriceYoy.setText(customerPriceYoy);
        } else {
            viewBinding.layoutCustomerPrice.textCustomerPriceYoy.setText("-");
        }
    }

    private void setStarSingleProductValue(GetSalesSummaryResp.ContentBean bean) {
        if (!TextUtils.isEmpty(bean.starpronumber)) {
            viewBinding.layoutStarSingleProduct.textStarSingleProductVolume.setText(bean.starpronumber);
        } else {
            viewBinding.layoutStarSingleProduct.textStarSingleProductVolume.setText("0");
        }

        if (!TextUtils.isEmpty(bean.daliytargetratio)) {
            viewBinding.layoutStarSingleProduct.textDayReach.setText(bean.daliytargetratio);
        } else {
            viewBinding.layoutStarSingleProduct.textDayReach.setText("-");
        }

        if (!TextUtils.isEmpty(bean.ztargetratio)) {
            viewBinding.layoutStarSingleProduct.textWeekReach.setText(bean.ztargetratio);
        } else {
            viewBinding.layoutStarSingleProduct.textWeekReach.setText("-");
        }
    }

    private void setUnitPriceValue(GetSalesSummaryResp.ContentBean bean) {
        if (!TextUtils.isEmpty(bean.zdj)) {
            viewBinding.layoutUnitPrice.textUnitPrice.setText(bean.zdj);
        } else {
            viewBinding.layoutUnitPrice.textUnitPrice.setText("0");
        }
    }

    @Override
    public void getDailySalesSummaryFailure(String msg) {
        Timber.tag("hhh").e("getDailySalesSummaryFailure msg= " + msg);
        if (TextUtils.isEmpty(msg)) {
            msg = "请求失败,请稍后重试";
        }
        ToastUtil.showToast(getContext(), msg);
        setDefaultValue();
    }

    @Override
    public void getDailySalesSummaryEmpty() {
        Timber.tag("hhh").e("getDailySalesSummaryEmpty");
        setDefaultValue();
    }

    private void setDefaultValue() {
        viewBinding.dailySeekBar.setProgress(0);
        viewBinding.weeklySeekBar.setProgress(0);
        viewBinding.layoutSalesAmount.textSalesAmount.setText("0");
        viewBinding.layoutSalesAmount.textSalesProportion.setText("-");
        viewBinding.layoutSalesAmount.textSalesMom.setText("-");
        viewBinding.layoutSalesAmount.textSalesYoy.setText("-");
        viewBinding.layoutSalesVolume.textSalesVolume.setText("0");
        viewBinding.layoutSalesVolume.textVolumeProportion.setText("-");
        viewBinding.layoutSalesVolume.textVolumeMom.setText("-");
        viewBinding.layoutSalesVolume.textVolumeYoy.setText("-");
        viewBinding.layoutMemberSalesAmount.textMemberSalesAmount.setText("0");
        viewBinding.layoutMemberSalesAmount.textMemberSalesProportion.setText("-");
        viewBinding.layoutMemberSalesAmount.textMemberSalesMom.setText("-");
        viewBinding.layoutMemberSalesAmount.textMemberSalesYoy.setText("-");
        viewBinding.layoutMemberSalesVolume.textMemberSalesVolume.setText("0");
        viewBinding.layoutMemberSalesVolume.textMemberVolumeProportion.setText("-");
        viewBinding.layoutMemberSalesVolume.textMemberVolumeMom.setText("-");
        viewBinding.layoutMemberSalesVolume.textMemberVolumeYoy.setText("-");
        viewBinding.layoutJointRate.textJointRate.setText("-");
        viewBinding.layoutJointRate.textJointRateMom.setText("-");
        viewBinding.layoutJointRate.textJointRateYoy.setText("-");
        viewBinding.layoutCustomerPrice.textCustomerPrice.setText("0");
        viewBinding.layoutCustomerPrice.textCustomerPriceMom.setText("-");
        viewBinding.layoutCustomerPrice.textCustomerPriceYoy.setText("-");
        viewBinding.layoutStarSingleProduct.textStarSingleProductVolume.setText("0");
        viewBinding.layoutStarSingleProduct.textDayReach.setText("-");
        viewBinding.layoutStarSingleProduct.textWeekReach.setText("-");
        viewBinding.layoutUnitPrice.textUnitPrice.setText("0");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (null != mPresenter) {
            mPresenter.unSubscribe();
        }
    }

    @Override
    public boolean isActive() {
        return false;
    }

    @Override
    public <T> LifecycleTransformer<T> bindToLife() {
        return this.bindToLifecycle();
    }
}
